package com.example.user_module.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;
import androidx.viewbinding.ViewBinding;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.example.base_application.BaseApplication;
import com.example.common.EvenBusAction;
import com.example.common.mvvm.view.BaseActivity;
import com.example.user_module.R;
import com.example.user_module.dao.UserDataBase;
import com.example.user_module.dao.UserEntity;
import com.example.user_module.databinding.ActivityLoginBinding;
import com.example.user_module.task.TaskActivity;
import com.google.android.material.button.MaterialButton;
import com.tencent.connect.UserInfo;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

@Route(path = "/user/LoginActivity")
public class LoginActivity extends BaseActivity<ActivityLoginBinding,LoginViewMoel> implements IUiListener , View.OnClickListener {
    private Tencent tencent;
    private String openid;
    private long expires_time;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void finishLogin(EvenBusAction evenBusAction){
        if (evenBusAction.action.equals("refresh")){
            finish();
        }
    }
    //授权完成
    @Override
    public void onComplete(Object o) {
        JSONObject jsonObject = (JSONObject) o;
        Iterator<String>it = jsonObject.keys();
        Log.e("www","jsonObject:"+jsonObject.toString());
        while (it.hasNext()) {
            String key = it.next();
            //用户登录接口返回值
            if (key.equals("openid")){
                try {
                    openid = jsonObject.getString("openid");
                    String access_token = jsonObject.getString("access_token");
                    String expires_in = jsonObject.getString("expires_in");
                    expires_time = jsonObject.getLong("expires_time");
                    tencent.setAccessToken(access_token,expires_in);
                    tencent.setOpenId(openid);
                    //配置获取qq用户信息请求
                    UserInfo info = new UserInfo(this,tencent.getQQToken());
                    info.getUserInfo(this);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return;
            }
        }
        try {
            String figureurl = jsonObject.getString("figureurl");
            String nickname = jsonObject.getString("nickname");
            String constellation = jsonObject.getString("constellation");
            vm.insertUser(figureurl,expires_time,0,0,nickname,openid,0);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onError(UiError uiError) {

    }

    @Override
    public void onCancel() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == Constants.REQUEST_LOGIN) {
            Tencent.onActivityResultData(requestCode, resultCode, data, this);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBinding() {
        ActivityLoginBinding binding=ActivityLoginBinding.inflate(inflater);
        return binding;
    }

    @Override
    public void initView() {
        v.actionClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        v.actionLogin.setOnClickListener(this);
    }


    @Override
    public void initData() {
        tencent = Tencent.createInstance("101794421",getApplicationContext());
    }

    @Override
    public void onClick(View view) {
        if (view==v.actionLogin){
            tencent.login(LoginActivity.this,"all",this);
        }
    }
}
